package com.yonyou.pmclouds.projectstatus.rmiimpl;

import com.alibaba.dubbo.config.annotation.Service;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.util.CommonFieldConst;
import com.yonyou.pmclouds.projectstatus.entity.ProjectStatusOutViewVO;
import com.yonyou.pmclouds.projectstatus.entity.ProjectStatusVO;
import com.yonyou.pmclouds.projectstatus.mapper.ProjectStatusMapper;
import com.yonyou.pmclouds.projectstatus.rmiitf.ProjectStatusMaintain;
import com.yonyou.pmclouds.projectstatus.rmiitf.ProjectStatusOutterMaintain;
import com.yonyou.pmclouds.projectstatus.rmiitf.ProjectStatusQuery;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

@Service(version = ApplicationConsts.APPLICATION_VERSION, interfaceClass = ProjectStatusOutterMaintain.class)
public class ProjectStatusOutterMaintainImpl implements ProjectStatusOutterMaintain {
    //需要更新的字段
    private static final String[] UPDATE_FIELDS = {ProjectStatusVO.SNAME,
            ProjectStatusVO.SCODE,ProjectStatusVO.ENABLE_STATUS, CommonFieldConst.MODIFIER, CommonFieldConst.MODIFYTIME};
    private static final String[] OUTTER_UPDATE_FIELDS = {ProjectStatusVO.SNAME,
            ProjectStatusVO.SCODE,ProjectStatusVO.ENABLE_STATUS, CommonFieldConst.MODIFIER, CommonFieldConst.MODIFYTIME};
    @Autowired
    private ProjectStatusMapper mapper;
    @Autowired
    private ProjectStatusQuery projectStatusQuery;
    @Autowired
    private ProjectStatusMaintain projectStatusMaintain;


    @Override
    public ProjectStatusVO insertFromOutter(ProjectStatusVO projectStatusVO) throws BusinessException {
        ProjectStatusVO origVO = projectStatusQuery.queryByPkSrc(projectStatusVO.getPkSrc());
        if(origVO != null)
            return origVO;
        return projectStatusMaintain.insert(projectStatusVO);
    }

    @Override
    public String deleteFromOutter(ProjectStatusVO projectStatusVO) throws BusinessException {
        ProjectStatusVO origVO = projectStatusQuery.queryByPkSrc(projectStatusVO.getPkSrc());
        return projectStatusMaintain.delete(origVO.getPkProjectStatus(), origVO.getChangeFlag());
    }

    @Override
    public ProjectStatusVO updateFromOutter(ProjectStatusVO projectStatusVO) throws BusinessException {
        ProjectStatusVO origVO = projectStatusQuery.queryByPkSrc(projectStatusVO.getPkSrc());

        if(origVO == null){
            return projectStatusMaintain.insert(projectStatusVO);
        }else{
            origVO.setScode(projectStatusVO.getScode());
            origVO.setSname(projectStatusVO.getSname());
            origVO.setEnableStatus(projectStatusVO.getEnableStatus());
            origVO.setStateSort(projectStatusVO.getStateSort());
            projectStatusMaintain.update(origVO);
            return origVO;
        }
    }



    @Override
    public ProjectStatusOutViewVO updateFromOutter(ProjectStatusOutViewVO projectStatusOutViewVO) throws BusinessException {

        ProjectStatusVO[] insertStatusVOs = projectStatusOutViewVO.getAddObjs();
        ProjectStatusVO[] updateStatusVOs = projectStatusOutViewVO.getUpdObjs();
        ProjectStatusVO[] deleteStatusVOs = projectStatusOutViewVO.getDelObjs();

        String[] pkSrcs = getPkSrcSet(insertStatusVOs, updateStatusVOs, deleteStatusVOs);

        Map<String, ProjectStatusVO> pkSrcAndVOMap = getStatusVOsBySrcs(pkSrcs);

        List<ProjectStatusVO> finalInsertList = new ArrayList<ProjectStatusVO>();
        List<ProjectStatusVO> finalUpdateList = new ArrayList<ProjectStatusVO>();
        List<ProjectStatusVO> finalDeleteList = new ArrayList<ProjectStatusVO>();

        if(ArrayUtils.isNotEmpty(insertStatusVOs)){
            for (ProjectStatusVO insertStatusVO:
                    insertStatusVOs) {
                if(pkSrcAndVOMap.get(insertStatusVO.getPkSrc())  == null)
                    finalInsertList.add(insertStatusVO);
            }
        }

        if(ArrayUtils.isNotEmpty(updateStatusVOs)){
            for (ProjectStatusVO updateStatusVO:
                    updateStatusVOs) {
                if(pkSrcAndVOMap.get(updateStatusVO.getPkSrc())  != null) {
                    updateStatusVO.setPkProjectStatus(pkSrcAndVOMap.get(updateStatusVO.getPkSrc()).getPkProjectStatus());
                    updateStatusVO.setChangeFlag(pkSrcAndVOMap.get(updateStatusVO.getPkSrc()).getChangeFlag());

                    finalUpdateList.add(updateStatusVO);
                }else{
                    finalInsertList.add(updateStatusVO);
                }
            }
        }

        if(ArrayUtils.isNotEmpty(deleteStatusVOs)){
            for (ProjectStatusVO deleteStatusVO:
                    deleteStatusVOs) {
                if(pkSrcAndVOMap.get(deleteStatusVO.getPkSrc())  != null) {
                    finalDeleteList.add(pkSrcAndVOMap.get(deleteStatusVO.getPkSrc()));
                }
            }
        }

        ProjectStatusOutViewVO resultVO = batchOperateStatusVOs(finalInsertList, finalUpdateList, finalDeleteList);

        return resultVO;
    }

    /**
     * 批量操作项目状态VO
     * @param finalInsertList
     * @param finalUpdateList
     * @param finalDeleteList
     * @return
     * @throws BusinessException
     */
    private ProjectStatusOutViewVO batchOperateStatusVOs(List<ProjectStatusVO> finalInsertList, List<ProjectStatusVO> finalUpdateList, List<ProjectStatusVO> finalDeleteList) throws BusinessException {
        ProjectStatusOutViewVO resultVO = new ProjectStatusOutViewVO();
        if(finalInsertList != null && finalInsertList.size()>0){
            resultVO.setAddObjs(finalInsertList.toArray(new ProjectStatusVO[0]));
            for (ProjectStatusVO statusVO :
                    finalInsertList) {
                projectStatusMaintain.insert(statusVO);
            }
        }

        if(finalUpdateList != null && finalUpdateList.size()>0){
            resultVO.setUpdObjs(finalUpdateList.toArray(new ProjectStatusVO[0]));
            for (ProjectStatusVO statusVO :
                    finalUpdateList) {
                projectStatusMaintain.update(statusVO);
            }
        }

        if(finalDeleteList != null && finalDeleteList.size()>0){
            resultVO.setDelObjs(finalDeleteList.toArray(new ProjectStatusVO[0]));
            for (ProjectStatusVO statusVO :
                    finalDeleteList) {
                projectStatusMaintain.delete(statusVO.getPkProjectStatus(),statusVO.getChangeFlag());
            }
        }
        return resultVO;
    }

    /**
     * 根据来源主键集合，查询已存在的项目状态VO
     * @param pkSrcs
     * @return
     * @throws BusinessException
     */
    private Map<String, ProjectStatusVO> getStatusVOsBySrcs(String[] pkSrcs) throws BusinessException {
        ProjectStatusVO[] origVOs = projectStatusQuery.queryByPkSrcs(pkSrcs);

        Map<String,ProjectStatusVO> pkSrcAndVOMap = new HashMap<String,ProjectStatusVO>();

        if(ArrayUtils.isEmpty(origVOs))
            return pkSrcAndVOMap;

        for (ProjectStatusVO origVO:
                origVOs) {
            pkSrcAndVOMap.put(origVO.getPkSrc(),origVO);
        }
        return pkSrcAndVOMap;
    }

    /**
     * 获取来源主键集合
     * @param insertStatusVOs
     * @param updateStatusVOs
     * @param deleteStatusVOs
     * @return
     */
    private String[] getPkSrcSet(ProjectStatusVO[] insertStatusVOs, ProjectStatusVO[] updateStatusVOs, ProjectStatusVO[] deleteStatusVOs) {
        Set<String> pkSrcSet = new HashSet<String>();

        if(ArrayUtils.isNotEmpty(deleteStatusVOs)){
            for (ProjectStatusVO deleteStatusVO:
                    deleteStatusVOs) {
                pkSrcSet.add(deleteStatusVO.getPkSrc()) ;
            }
        }
        if(ArrayUtils.isNotEmpty(insertStatusVOs)){
            for (ProjectStatusVO insertStatusVO:
                    insertStatusVOs) {
                pkSrcSet.add(insertStatusVO.getPkSrc()) ;
            }
        }
        if(ArrayUtils.isNotEmpty(updateStatusVOs)){
            for (ProjectStatusVO updateStatusVO:
                    updateStatusVOs) {
                pkSrcSet.add(updateStatusVO.getPkSrc()) ;
            }
        }
        return pkSrcSet.toArray(new String[0]);
    }
}
